<?php


namespace RedMVC\Form\Validator;

/**
 * StrLength class
 *  
 * @category    RedMVC
 * @package     Form
 * @subpackage  Validator 
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class StrLength extends AValidator{
    
    private $minStrLength = 1;
    private $maxStrLength = null;
    
    public function __construct($minStrLength, $maxStrLength = null){
        $this->minStrLength = $minStrLength;
        $this->maxStrLength = $maxStrLength;
    }
    
    public function isValid($value, $formValues = null){
        $value = trim($value);
        if(($this->minStrLength !== null) && (mb_strlen($value, 'UTF-8') < $this->minStrLength)){
            $this->setError('FORM_VALIDATOR_STR_LENGTH_MIN', $this->minStrLength);
            return false;
        }
        if(($this->maxStrLength !== null) && (mb_strlen($value, 'UTF-8') > $this->maxStrLength)){
            $this->setError('FORM_VALIDATOR_STR_LENGTH_MAX', $this->maxStrLength);
            return false;
        }
        return true;
    }
}